
#include "PCH.h"
#include "BMPHeader.h"
#include <stdlib.h>
#include "Color.h"
#include "Scene.h"
#include "Camera.h"
#include "Raytrace.h"
#include "Timer.h"
#include "MyListener.h"
#include "constants.h"
#include "Globals.h"

int main() {
	MyListener MyListen;
	Timer Chrono;
	CCamera camera;
	camera.SetFOV(50);
	static int counter =0;
	//CColor * fpixels = new CColor[ViewportWidth * ViewportHeight];
	unsigned int * pixels;// = new unsigned int[ViewportWidth * ViewportHeight];
	CScene Scene;
//	MyListen.SetScene(&Scene);
//	MyListen.SetCamera(&camera);
	Scene.LoadMaterials("Materials.txt");
	//Scene.SetupScene(12);
	Scene.LoadObjects("Scene.txt",&camera);
// 	camera.LookAt = CVector(0.0f, 0.0f, 0.0f);
// 	camera.Position = CVector(0,0,6000.0f);
	//camera.SetFOV(20);
	
	//PixelToaster::Display display( "Pixel Raytracer", ViewportWidth, ViewportHeight );
	//display.listener(&MyListen);
	//std::vector<PixelToaster::Pixel> pixels( ViewportWidth * ViewportHeight );

	//for(int FrameNumber =1;FrameNumber<3;++FrameNumber)
	{//int FrameNumber=93;
		// 	Chrono.reset();
		// 	while(display.open())
		// 	{
		// 	Globals::Instance().CollisionTime =0;
		// 	Globals::Instance().DiffuseTime =0;
		// 	Globals::Instance().ReflectionTime =0;
		// 	Globals::Instance().RefractionTime =0;
		// 	FrameNumber++;
		 	

		std::ostringstream filename;
		filename << "OUT5\\Test" << std::setfill('0') << std::setw(2) << 9 << "b.bmp";

		FILE * output = fopen(filename.str().c_str(), "wb");
		fwrite(&BMPHeader(ViewportWidth, ViewportHeight), sizeof(BMPHeader), 1, output);

		//CScene Scene;
		//	MyListen.SetScene(&Scene);
		//	MyListen.SetCamera(&camera);
		//Scene.LoadMaterials("Materials.txt");
		//Scene.SetupScene(FrameNumber);
		//		unsigned int *pixels;

		//		Chrono.reset();
		camera.RenderScene_RAW(Scene,pixels);
		// 		double RenderTime=Chrono.milliseconds();
		// 		Chrono.reset();


		// 		if(display.open())
		// 			display.update(pixels);
		// 		double DisplayTime=Chrono.milliseconds();

// 		double TotalTime;//=RenderTime+DisplayTime;
// 		int i=0;
		// 		while(i<(1.0*RenderTime/TotalTime*ViewportWidth))
		// 		{
		// 			pixels[i].r=1.0;
		// 			pixels[i].g=0.0;
		// 			pixels[i].b=0.0;
		// 			pixels[ViewportWidth+i].r=1.0;
		// 			pixels[ViewportWidth+i].g=0.0;
		// 			pixels[ViewportWidth+i].b=0.0;
		// 			pixels[ViewportWidth+ViewportWidth+i].r=1.0;
		// 			pixels[ViewportWidth+ViewportWidth+i].g=0.0;
		// 			pixels[ViewportWidth+ViewportWidth+i].b=0.0;
		// 			++i;
		// 		}
		// 		while(i<ViewportWidth)
		// 		{
		// 			pixels[i].r=0.0;
		// 			pixels[i].g=1.0;
		// 			pixels[i].b=0.0;
		// 			pixels[ViewportWidth+i].r=0.0;
		// 			pixels[ViewportWidth+i].g=1.0;
		// 			pixels[ViewportWidth+i].b=0.0;
		// 			pixels[ViewportWidth+ViewportWidth+i].r=0.0;
		// 			pixels[ViewportWidth+ViewportWidth+i].g=1.0;
		// 			pixels[ViewportWidth+ViewportWidth+i].b=0.0;
		// 			++i;
		// 		}
		// 		
		// 	double FrameTime=Chrono.seconds();
		// 	double FPS = 1.0/FrameTime;
		// 	for(int J =0; J<(int(FPS)/10);++J)
		// 	pixels[J*5].r=1.0;
		// 	for(int J =0; J<(int(FPS)%10);++J)
		// 	pixels[2*ViewportWidth+J*5].r=1.0;
		// 	TotalTime=Globals::Instance().CollisionTime+Globals::Instance().DiffuseTime+Globals::Instance().ReflectionTime+Globals::Instance().RefractionTime;
		// 	i=0;
		// 	while(i<(1.0*Globals::Instance().CollisionTime/TotalTime*ViewportWidth))
		// 	{
		// 	pixels[4*ViewportWidth+i].r=1.0;
		// 	pixels[4*ViewportWidth+i].g=0.0;
		// 	pixels[4*ViewportWidth+i].b=0.0;
		// 	pixels[5*ViewportWidth+i].r=1.0;
		// 	pixels[5*ViewportWidth+i].g=0.0;
		// 	pixels[5*ViewportWidth+i].b=0.0;
		// 	pixels[6*ViewportWidth+i].r=1.0;
		// 	pixels[6*ViewportWidth+i].g=0.0;
		// 	pixels[6*ViewportWidth+i].b=0.0;
		// 	++i;
		// 	}
		// 	while(i<(1.0*(Globals::Instance().CollisionTime+Globals::Instance().DiffuseTime)/TotalTime*ViewportWidth))
		// 	{
		// 	pixels[4*ViewportWidth+i].r=1.0;
		// 	pixels[4*ViewportWidth+i].g=1.0;
		// 	pixels[4*ViewportWidth+i].b=0.0;
		// 	pixels[5*ViewportWidth+i].r=1.0;
		// 	pixels[5*ViewportWidth+i].g=1.0;
		// 	pixels[5*ViewportWidth+i].b=0.0;
		// 	pixels[6*ViewportWidth+i].r=1.0;
		// 	pixels[6*ViewportWidth+i].g=1.0;
		// 	pixels[6*ViewportWidth+i].b=0.0;
		// 	++i;
		// 	}
		// 	while(i<(1.0*(Globals::Instance().CollisionTime+Globals::Instance().DiffuseTime+Globals::Instance().ReflectionTime)/TotalTime*ViewportWidth))
		// 	{
		// 	pixels[4*ViewportWidth+i].r=0.0;
		// 	pixels[4*ViewportWidth+i].g=1.0;
		// 	pixels[4*ViewportWidth+i].b=0.0;
		// 	pixels[5*ViewportWidth+i].r=0.0;
		// 	pixels[5*ViewportWidth+i].g=1.0;
		// 	pixels[5*ViewportWidth+i].b=0.0;
		// 	pixels[6*ViewportWidth+i].r=0.0;
		// 	pixels[6*ViewportWidth+i].g=1.0;
		// 	pixels[6*ViewportWidth+i].b=0.0;
		// 	++i;
		// 	}
		// 
		// 	while(i<(ViewportWidth))
		// 	{
		// 	pixels[4*ViewportWidth+i].r=0.0;
		// 	pixels[4*ViewportWidth+i].g=0.5;
		// 	pixels[4*ViewportWidth+i].b=1.0;
		// 	pixels[5*ViewportWidth+i].r=0.0;
		// 	pixels[5*ViewportWidth+i].g=0.5;
		// 	pixels[5*ViewportWidth+i].b=1.0;
		// 	pixels[6*ViewportWidth+i].r=0.0;
		// 	pixels[6*ViewportWidth+i].g=0.5;
		// 	pixels[6*ViewportWidth+i].b=1.0;
		// 	++i;
		// 	}
		// 	Chrono.reset();
		// 	if(display.open())
		// 	display.update(pixels);

		//std::cout << Chrono.seconds() << " sec de rendu " << std::endl;

		for(int y = 0; y < ViewportHeight; ++y) 
		{
			for(int x = 0; x < ViewportWidth; ++x) 
			{
				fwrite(pixels + x + y * ViewportWidth, 4, 1, output);
			}
		}
		delete[] pixels;
		fclose(output);
		ShellExecute(0,"Open",filename.str().c_str(),NULL,NULL,0);
	}
	//delete output;
	//}


	return 0;
}










